package dhm.com.hpx.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import dhm.com.hpx.R;
import dhm.com.hpx.base.BaseActivity;
import dhm.com.hpx.base.Presenter.PressenterImpl;
import dhm.com.hpx.base.netWork.Constant;
import dhm.com.hpx.base.netWork.LoginContract;
import dhm.com.hpx.entity.CatLListBean;
import dhm.com.hpx.fragment.SpeechcraftFragment;
import dhm.com.hpx.utils.SpUtils;
import dhm.com.hpx.utils.StringUtil;

public class SpeechcraftActivity extends BaseActivity implements LoginContract.IView {


    @BindView(R.id.tab_homefg)
    TabLayout tabHomefg;
    @BindView(R.id.vp_homefg)
    ViewPager vpHomefg;
    private List<CatLListBean.DataBean> tabEntities;
    private PressenterImpl pressenter;
    private String uid;

    private CategoryPageAdapter pageAdapter;
    private List<Fragment> fragments;

    @Override
    protected int getLayout() {
        return R.layout.activity_speechcraft;
    }

    @SuppressLint("NewApi")
    @Override
    protected void initView() {
        ButterKnife.bind(this);
        pressenter=new PressenterImpl();
        pressenter.attachView(this);
        fragments = new ArrayList<>();
        tabEntities = new ArrayList<>();
        uid = SpUtils.getString(this, "uid");

        pageAdapter = new CategoryPageAdapter(getSupportFragmentManager());
        vpHomefg.setAdapter(pageAdapter);
        tabHomefg.setupWithViewPager(vpHomefg);
        //tab的下划线颜色,默认是粉红色
        tabHomefg.setSelectedTabIndicatorColor(getResources().getColor(R.color.main));
    }

    @Override
    protected void onResume() {
        super.onResume();
        Map<String,String> map=new HashMap<>();
        map.put("token", Constant.TOKEN);
        map.put("uid",uid);
        pressenter.sendMessage(this,Constant.cat_list,map, CatLListBean.class);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void requesta(Object data) {
        if (data instanceof CatLListBean){
            CatLListBean catLListBean= (CatLListBean) data;
            if (catLListBean.getCode()==1){
                tabEntities.clear();
                fragments.clear();
                tabEntities.addAll(catLListBean.getData());
                for (CatLListBean.DataBean dataBean : tabEntities) {
                    fragments.add(new SpeechcraftFragment(dataBean.getCat_id()));
                }
                pageAdapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    public void fail(String error) {

    }

    @OnClick({R.id.back, R.id.add})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.add:
                Intent intent=new Intent(this,SpeechcraftlistActivity.class);
                startActivity(intent);
                break;
                default:
        }
    }

    //内部类写viewPage适配器
    class CategoryPageAdapter extends FragmentPagerAdapter {

        public CategoryPageAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            return fragments.get(i);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return StringUtil.preventNull(tabEntities.get(position).getCat_name());
        }
    }

}
